#include "mycoin.h"

MyCoin::MyCoin(QWidget *parent) : QPushButton(parent)
{

}

MyCoin::MyCoin(QString coinImage, int flag){
    this->flag = flag;
    this->btnWin = false;
    QPixmap pixmap;
    bool result = pixmap.load(coinImage);
    if(!result){
        qDebug()<<"image load error";
        return;
    }
    this->setFixedSize(QSize(pixmap.width(), pixmap.height()));
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pixmap);
    this->setIconSize(QSize(pixmap.width(), pixmap.height()));

    // 转到银币
    trans = new QTimer;
    // 转到金币
    retrans = new QTimer;

    connect(trans, &QTimer::timeout, this, [=](){

        QPixmap map;
        QString src = QString(":/Coin000%1.png").arg(this->min++);
        map.load(src);


        this->setFixedSize(QSize(map.width(), map.height()));
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(map);
        this->setIconSize(QSize(map.width(), map.height()));

        // 如果已经切换到最后一张
        if(this->min > this->max){
            this->min = 1;
            trans->stop();
        }

    });

    connect(retrans, &QTimer::timeout, this, [=](){

        QPixmap map;
        QString src = QString(":/Coin000%1.png").arg(this->max--);
        map.load(src);


        this->setFixedSize(QSize(map.width(), map.height()));
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(map);
        this->setIconSize(QSize(map.width(), map.height()));

        // 如果已经切换到最后一张
        if(this->max < this->min){
            this->max = 8;
            retrans->stop();
        }

    });



}

void MyCoin::changeStatus(){

    if(this->flag == 1){

        trans->start(30);
        this->flag = 0;

    }else{

        retrans->start(30);
        this->flag = 1;

    }

}

void MyCoin::mousePressEvent(QMouseEvent* event){
    if(this->btnWin){
        return;
    }else{
        QPushButton::mousePressEvent(event);
    }
}
